Visual Basic Concepts

Adding Events to a Class

Okay, let's say you've created a dinosaur simulation, complete with Stegosaur, Triceratops, and Tyrannosaur classes. As the final touch, you want the Tyrannosaur to roar, and when it does you want every other dinosaur in your simulation to sit up and take notice.

If the Tyrannosaur class had a Roar event, you could handle that event in all your other dinosaur classes. This topic discusses the declaration and handling of events in your class modules.

Note   Kids, don't try this at home, at least with more than a few dinosaurs. Connecting every dinosaur with every other dinosaur using events could make your dinosaurs so slow that mammal objects would take over the simulation.

Properties and methods are said to belong to incoming interfaces, because they're invoked from outside the object. By contrast, events are called outgoing interfaces, because they're initiated within the object, and handled elsewhere.

The following topics describe the process of declaring, raising, and handling events, with examples.

For More Information   Creating ActiveX Components, in the Component Tools Guide provided with the Professional and Enterprise editions, discusses the use of events in designing your own software components.

For a discussion of a better way to handle dinosaurs, see "Polymorphism" later in this chapter.